﻿Imports StackExchange.Redis
Imports Newtonsoft.Json

Public Class RedisUtil
    Dim gConnstr As String = ""

    Public Sub init(connstr As String)
        gConnstr = connstr
    End Sub
    Public Function getConn() As ConnectionMultiplexer
        Return ConnectionMultiplexer.Connect(gConnstr)
    End Function
    Public Sub subscribe(chn As String, act As Action(Of RedisChannel, RedisValue))
        Using rc As ConnectionMultiplexer = getConn()
            rc.GetSubscriber.Subscribe(chn, act)
        End Using
    End Sub
    Public Sub publish(msg As MsgObj)
        Using rc As ConnectionMultiplexer = getConn()
            rc.GetSubscriber.Publish(msg.sRec, encMsg(msg))
        End Using
    End Sub
    Public Function encMsg(msg As MsgObj) As String
        Return JsonConvert.SerializeObject(msg).ToString
    End Function
    Public Function decMsg(msg As String) As MsgObj
        Return JsonConvert.DeserializeObject(Of MsgObj)(msg)
    End Function
End Class
